package cn.rwklyd.BookKeeping.controller;

import cn.rwklyd.BookKeeping.pojo.Platform;
import cn.rwklyd.BookKeeping.pojo.Result;
import cn.rwklyd.BookKeeping.pojo.dto.QueryPlatforms;
import cn.rwklyd.BookKeeping.service.PlatformService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/platform")
@Tag(name = "平台管理", description = "平台的增删查改接口")
@Slf4j
public class PlatformController {

    @Autowired
    private PlatformService platformService;

    /**
     * 添加平台
     */
    @PostMapping("/add")
    @Operation(summary = "添加平台", description = "添加一个新的平台")
    @PreAuthorize("hasAuthority('view_users')")
    public Result<String> addPlatform(@RequestBody Platform platform) {
        log.info("添加平台: {}", platform);
        return platformService.addPlatform(platform);
    }

    /**
     * 删除平台
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除平台", description = "根据ID删除平台")
    @PreAuthorize("hasAuthority('view_users')")
    public Result<String> deletePlatform(
            @Parameter(description = "平台ID", required = true)
            @PathVariable Long id) {
        log.info("删除平台: {}", id);
        return platformService.deletePlatform(id);
    }

    /**
     * 更新平台
     */
    @PostMapping("/update")
    @Operation(summary = "更新平台", description = "更新平台信息")
    @PreAuthorize("hasAuthority('view_users')")
    public Result<String> updatePlatform(@RequestBody Platform platform) {
        log.info("更新平台: {}", platform);
        return platformService.updatePlatform(platform);
    }

    /**
     * 多条件查询平台
     */
    @PostMapping("/query")
    @Operation(summary = "查询平台", description = "多条件查询平台，不传参数则查询所有")
    @PreAuthorize("hasAuthority('view_users')")
    public Result<IPage<Platform>> queryPlatforms(@RequestBody QueryPlatforms queryPlatforms) {
        log.info("查询平台参数: {}", queryPlatforms);
        Long id = queryPlatforms.getId();
        String name = queryPlatforms.getName();
        Long userId = queryPlatforms.getUserId();
        Integer page = queryPlatforms.getPage() != null ? queryPlatforms.getPage() : 1;
        Integer pageSize = queryPlatforms.getPageSize() != null ? queryPlatforms.getPageSize() : 10;
        return platformService.queryPlatforms(id, name, userId, page, pageSize);
    }
    
    /**
     * 根据ID获取平台
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取平台", description = "根据ID获取平台详情")
    @PreAuthorize("hasAuthority('view_users')")
    public Result<Platform> getPlatformById(
            @Parameter(description = "平台ID", required = true)
            @PathVariable Long id) {
        log.info("获取平台: {}", id);
        return platformService.getPlatformById(id);
    }

    /**
     * 更新平台状态（启用/禁用切换）
     */
    @PostMapping("/updateStatus/{id}")
    @Operation(summary = "切换平台状态", description = "切换平台的启用/禁用状态")
    @PreAuthorize("hasAuthority('view_users')")
    public Result<String> updatePlatformStatus(
            @Parameter(description = "平台ID", required = true)
            @PathVariable Long id) {
        log.info("切换平台状态: id={}", id);
        return platformService.updatePlatformStatus(id);
    }
    
    /**
     * 获取所有平台
     */
    @GetMapping("/all")
    @Operation(summary = "获取所有平台", description = "获取当前用户可见的所有平台")
    @PreAuthorize("hasAuthority('view_users')")
    public Result<List<Platform>> getAllPlatforms() {
        log.info("获取所有平台");
        return platformService.getAllPlatforms();
    }
    
    /**
     * 分页获取平台列表
     */
    @GetMapping("/list")
    @Operation(summary = "分页获取平台", description = "分页获取当前用户的平台列表")
    @PreAuthorize("hasAuthority('view_users')")
    public Result<IPage<Platform>> getPlatformList(
            @Parameter(description = "页码") @RequestParam(required = false) Integer page,
            @Parameter(description = "每页大小") @RequestParam(required = false) Integer pageSize,
            @Parameter(description = "平台名称") @RequestParam(required = false) String name) {
        log.info("分页获取平台列表: page={}, pageSize={}, name={}", page, pageSize, name);
        return platformService.getPlatformList(page, pageSize, name);
    }
} 